<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<?php include "../public/header_main.php";?>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">

    <div class="page-title">
        <div class="page-title-con"><span>编辑材料费清单</span><div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" key="temp" lay-filter="add">保存</button>
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">提交</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container" style="padding-bottom: 0!important;">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div id="editshi" class="float-r colorlan"></div>
                <div class="budopt"></div>
                <div class="budbox" >
                    <table id="budtable" class="tablewid" openkey="0" onselectstart="return false"></table>
                </div>
            </div>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;


    function check_main_base_cai(key, field) {
        openPage('../public/check_main_base_cai.php?page=main_mark_cai_qing&rowkey='+key+'&funcname=assign_source','80%','80%')
    }
    function assign_source(rowkey, data) {
        initObj.data[rowkey]['source'] = data.id;
        initObj.data[rowkey]['noratefee'] = data.noratefee;
        initObj.data[rowkey]['sourceInfo'] = data;
        list_list();
    }
    function empty_source(key, field) {
        initObj.data[key][field] = '';
        initObj.data[key]['sourceInfo'] = '';
        list_list();
    }


    var tabHeader;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_main_mark_cai_qing,data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#editshi").html(res.data.editshi);


                tabHeader = [
                    {type:'numbers',title:'序号'},
                    {field:'basetype',title:'类别',type:'xmdan',xmdata:res.data.basetype},
                    {field:'name',title:'名称',required:true},
                    {field:'guige',title:'特征描述'},
                    {field:'valunit',title:'单位'},
                    {field:'hanexp',title:'含量表达式',default:'=1'},
                    {field:'hannum',title:'含量',default:1,disabled:true,math:'hanexp'},
                    {field:'sunrate',title:'损耗率',default:'0'},
                    {field:'noratefee',title:'不含税单价',type:'number'},
                    {field:'chengfee',title:'成本单价',type:'number',disabled:true,mathduo:'【noratefee】*(1+【sunrate】)*【hannum】'},
                    {field:'explain',title:'备注'},
                    {field:'source',title:'溯源', type:'func',callback:function (key, field, value) {
                        var str = '<span class="colorlan ml5" onclick="check_main_base_cai(\''+key+'\',\''+field+'\')">选择</span>';
                        if(initObj.data[key].sourceInfo){
                            str += '<span class="colorlan ml5" onclick="openPage(\'../main_base_cai/show.php?id='+initObj.data[key].sourceInfo.id+'\')">查看</span>';
                            str += '<span class="colorlan ml5" onclick="empty_source(\''+key+'\',\''+field+'\')">清空</span>';
                        }
                        return str;
                    }},
                ]
                init({dom:'budtable', header:tabHeader, data: res.data.list,});

                render_date();
                render_from();
                initParseMoney();
            }
        });

        form.on('submit(add)', function(data){
            var keyname = $(this).attr('key') || '';
            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}

            var listArr = getBudListData();
            var list = [''];
            try {
                for (key in listArr){
                    var index = parseInt(key) + 1;
                    if(!listArr[key].name){
                        throw new Error('第'+index+'行，请输入名称');
                    }
                    list.push({
                        basetype: listArr[key].basetype,
                        name: listArr[key].name,
                        guige: listArr[key].guige,
                        valunit: listArr[key].valunit,
                        hanexp: listArr[key].hanexp,
                        hannum: listArr[key].hannum,
                        sunrate: listArr[key].sunrate,
                        noratefee: listArr[key].noratefee,
                        chengfee: listArr[key].chengfee,
                        source: listArr[key].source,
                        explain: listArr[key].explain,
                    });
                }
            } catch(e) {
                facade.error(e.message);
                return false;
            }
            data.field.list = list;
            data.field.id = param_id;
            facade.ajax({url: ask_main_mark_cai_qing, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    if(keyname === 'temp'){
                        facade.success("保存成功!");
                        $("#editshi").html(res.data.editshi);
                    }else{
                        dataPostTimeOut();
                    }
                }
            });

            return false;
        });

    });



</script>
</body>
</html>
